<html>
<head><meta charset="utf-8"><title>Getting code representation of expr from HIR clippy#6713 · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html">Getting code representation of expr from HIR clippy#6713</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225959941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225959941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225959941">(Feb 11 2021 at 08:25)</a>:</h4>
<p>Hey, originally I asked this in <a class="stream" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp">#t-compiler/help</a> but i've been told that it could also be appropriate here:</p>
<p><span class="user-mention silent" data-user-id="372858">boolean_coercion</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Getting.20code.20representation.20of.20expression.20from.20HIR/near/225927435">said</a>:</p>
<blockquote>
<p>I'm trying to make a clippy lint that warns against the usage of e.g. <code>u16::from_str_radix(string, 10)</code> (the 10 is specific) and instead replace it with <code>string.parse()</code>.</p>
<p>To recommend the second option though, I need to have access to how the original expression that produced <code>string</code> looks like. <del>I'm asking this here instead of at <a class="stream" data-stream-id="257328" href="/#narrow/stream/257328-clippy">#clippy</a> , because</del> <code>LateLintPass</code> only gives me access to a <code>rustc_hir::Expr</code> and a <code>LateLintContext</code> when linting on expressions<del>, which seem to come from the compiler and are not specific to clippy</del>.</p>
<p>Is there any way that I can do what I want?</p>
</blockquote>
<p>By the way, it looks like the appropriate <a href="https://github.com/rust-lang/rust-clippy/issues/6713">issue</a> has been missed, could someone take a look and give their opinion? <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span></p>



<a name="225960055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225960055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225960055">(Feb 11 2021 at 08:26)</a>:</h4>
<p>To clarify:</p>
<blockquote>
<p>I meant, for example, getting a string from some function invocation (Call or MethodCall) instead of a variable (or QPath, if I understand that part of HIR correctly) in the first argument of the function, and then using what the user wrote to suggest an alternative.</p>
<p>Those aren't the only two cases of course, and I'm sure there's a better way than stringifying each type of Expr manually <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>
</blockquote>



<a name="225960563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225960563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225960563">(Feb 11 2021 at 08:33)</a>:</h4>
<p>Welcome <span class="user-mention" data-user-id="372858">@boolean_coercion</span> to clippy. I've changed the topic headline a bit to link to the issue I hope that's okay. </p>
<p>Regarding your question, do you want to get the declaration of the used value or simply the code that the user as written to get the string? <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">input</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="n">get_input</span><span class="p">();</span><span class="w"> </span><span class="c1">// Do you want this?</span>
<span class="kd">let</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">u16</span>::<span class="n">from_str_radix</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="c1">//                            ^^^^^ or this?</span>
</code></pre></div>



<a name="225961392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225961392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225961392">(Feb 11 2021 at 08:43)</a>:</h4>
<p>If you want to get the second case so this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">u16</span>::<span class="n">from_str_radix</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="c1">//                            ^^^^^ or this?</span>
</code></pre></div>
<p>You can look at <a href="https://github.com/rust-lang/rust-clippy/blob/3784cdf98ed7b23e698e2aa63f08bb05937b68b1/clippy_lints/src/utils/mod.rs#L628"><code>clippy_lints::utils::snippet</code></a> this function allows you to get the code as a string of a hir item. In this case you could take the first function parameter of <code>u16::from_str_radix</code> and simply use its span to get <code>input</code> as a string</p>



<a name="225962149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225962149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225962149">(Feb 11 2021 at 08:51)</a>:</h4>
<p>Yes, that's exactly what I needed!! Thank you <span class="user-mention" data-user-id="369415">@xFrednet</span> :D</p>



<a name="225962309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Getting%20code%20representation%20of%20expr%20from%20HIR%20clippy%236713/near/225962309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Getting.20code.20representation.20of.20expr.20from.20HIR.20clippy.236713.html#225962309">(Feb 11 2021 at 08:53)</a>:</h4>
<p>Awesome, you're welcome :D. I've also left a comment in the issue saying that the lint makes sense to me and a tip how you can assign the issue to yourself <span class="user-mention" data-user-id="372858">@boolean_coercion</span>  :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>